<?php
class MascotasController extends AppController
{
    public $helpers = array(
        'Html',
        'Form',
        'Session',
        'Js');
    public $uses = array(
        'Criadero',
        'Propietario',
        'Criadero',
        'Mascota',
        'Raza');
    public $layout = 'default';
    public $component = array('Session');

    public function index()
    {

        $mascotas = $this->Mascota->find('all', array(
            'recursive' => 2,
            'order' => 'Mascota.id DESC',
            'limit' => 15));
        //debug($mascotas);exit;
        foreach($mascotas as $mascota){
        //debug($mascota);
        $duenos = $mascota['Duenosmascota'];
        //debug($duenos);
        
        }
        
        
        
        $this->set(compact('mascotas', 'duenos'));
        

    }

    public function insertar()
    {

        if (!empty($this->data)) {
            //debug($this->data);
            $this->Mascota->create();
            if ($this->Mascota->save($this->data)) {
                //echo 'guardo';
                $this->Session->setFlash('Registro Exitosamente');
                $this->redirect(array('action' => 'index'));
            } else {
                //echo 'No guardo';
            }
        } else {

        }
        
        $dcr = $this->Raza->find('list', array('recursive' => -1, 'fields' => array('nombre')));
        $dcp = $this->Propietario->find('list', array('recursive' => -1, 'fields' => array('nombre')));
        $dcm = $this->Mascota->find('list', array('recursive' => -1, 'fields' => array('nombre')));
        $criaderos = $this->Criadero->find('list', array('fields'=>array('Criadero.id', 'Criadero.nombre')));
        $this->set(compact('dcr', 'dcp', 'dcm', 'criaderos'));
    }

    public function editar($id = null)
    {

        $this->Mascota->id = $id;
        if (!$id) {
            $this->Session->setFlash('No existe tal registro');
            $this->redirect(array('action' => 'index'), null, true);
        }
        if (empty($this->data)) {
            $this->data = $this->Mascota->read(); //find(array('id' => $id));
        } else {
            
            if ($this->Mascota->save($this->data)) {
                $this->Session->setFlash('Los datos fueron modificados');
                $this->redirect(array('action' => 'index'), null, true);
            } else {
                $this->Session->setFlash('no se pudo modificar!!');
            }
        }
        $dcr = $this->Raza->find('list', array('recursive' => -1, 'fields' => array('nombre')));
        $dcp = $this->Propietario->find('list', array('recursive' => -1, 'fields' => array('nombre')));
        $dcm = $this->Mascota->find('list', array('recursive' => -1, 'fields' => array('nombre')));
        $this->set(compact('dcr', 'dcp', 'dcm'));

    }

}
